package edu.fpa.day0804;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

public class Send_TMultiClient3 {
    private BufferedReader console;
    DataOutputStream dos;
    private Socket client;
    private boolean isRunning;
    private String name;
    public Send_TMultiClient3(Socket client,String name) {
        this.client = client;
        console = new BufferedReader(new InputStreamReader(System.in));
        this.isRunning = true;
        this.name = name;
        try {
            dos = new DataOutputStream(client.getOutputStream());
            //发送名称
            send(name);
        } catch (IOException e) {
            System.out.println("---error---");
            this.release();
        }
    }
    public void run() {
        while(isRunning) {
            String msg = getStrFromConsole();
            if(msg.equals("")) {
                send(msg);
            }
        }
    }
    //发送消息
    private void send(String msg) {
        try {
            dos.writeUTF(msg);
            dos.flush();
        } catch (IOException e) {
            System.out.println("-----send error-----");
            release();
        }
    }
    /**
     * 从控制台获取消息
     * @return
     */
    private String getStrFromConsole() {
        try {
            return console.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
    //释放资源
    private void release() {
        this.isRunning = false;
        Utils_TMultiChat3.close(dos,client);
    }
}
